//
//  BrowserViewController_iPhone.m
//  iFlick1.4
//
//  Created by vhly on 10-9-11.
//  Copyright 2010 xhzhang. All rights reserved.
//

#import "BrowserViewController_iPhone.h"
#import "FlickrClient.h"


@implementation BrowserViewController_iPhone

@synthesize web;
@synthesize requestURL;
@synthesize delegate;

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated{
	[super viewWillAppear:animated];
	if (delegate) {
		UIBarButtonItem *btnOK = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonItemStyleDone target:self action:@selector(doFinish:)];
		self.navigationItem.rightBarButtonItem = btnOK;
		[btnOK release];
	}
	if (requestURL) {
		//isFirst = YES;
		NSURL *url = [[NSURL alloc] initWithString:requestURL];
		NSMutableURLRequest *req = [[NSMutableURLRequest alloc] initWithURL:url];
		[req setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];
		[req setValue:@"iFlick/1.4" forHTTPHeaderField:@"User-Agent"];
		[req setHTTPMethod:@"GET"];
		[web loadRequest:req];
		[req release];
		[url release];
		// [self.view addSubview:vProcess];
	}
}

- (void)viewDidDisappear:(BOOL)animated{
	[super viewDidDisappear:animated];
	[web stopLoading];
	// isFirst = YES;
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    if ([web isLoading]) {
		[web stopLoading];
	}
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)webViewDidStartLoad:(UIWebView *)webView{
	FlickrClient *client = [FlickrClient getInstance];
	[client incNetworkRef];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView{
	FlickrClient *client = [FlickrClient getInstance];
	[client decNetworkRef];
	//if (isFirst) {
//		[vProcess removeFromSuperview];
//		isFirst = NO;
//	}
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{

}

- (void)doFinish:(id)sender{
	[self.navigationController popViewControllerAnimated:YES];
	[NSThread detachNewThreadSelector:@selector(browseFinished) toTarget:delegate withObject:nil];
}

- (void)dealloc {
	[web release];
    [super dealloc];
}


@end
